import numpy as np

if __name__ == '__main__':
    matrix_random = np.arange(0, 9).reshape((3, 3))
    print(matrix_random)

    matrix_triangle_1 = np.triu(matrix_random)
    print(matrix_triangle_1)

    #  k = 0 为默认值，保留主对角线上元素的值
    matrix_triangle_2 = np.triu(matrix_random, k=0)
    print(matrix_triangle_2)

    # k = 1 删除主对角线上元素
    matrix_triangle_3 = np.triu(matrix_random, k=1)
    print(matrix_triangle_3)

    # k = 2 删除主对角线上元素及其上面一层
    matrix_triangle_4 = np.triu(matrix_random, k=2)
    print(matrix_triangle_4)

    # k = 3 删除主对角线上元素及其上面两层
    matrix_triangle_5 = np.triu(matrix_random, k=3)
    print(matrix_triangle_5)

    # k = -1 保留主对角线上元素
    matrix_triangle_6 = np.triu(matrix_random, k=-1)
    print(matrix_triangle_6)